﻿#region copyright
//<copyright>
// Copyright(C) 2012 TrackerRealm Corporation
// This file is part of the open source project - Jazz. http://jazz.codeplex.com
// 
// Jazz is open software: you can redistribute it and/or modify it 
// under the terms of the GNU Affero General Public License (AGPL) as published by 
// the Free Software Foundation, version 3 of the License.
// 
// Jazz is distributed in the hope that it will be useful, 
// but WITHOUT ANY WARRANTY; without even the implied warranty 
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
//  See the GNU Affero General Public License (AGPL) for more details.
// 
// You should have received a copy of the GNU General Public 
// License along with Jazz.  If not, see <http://www.gnu.org/licenses/>.
//
// REMOVAL OF THIS NOTICE IS VIOLATION OF THE COPYRIGHT. 
//</copyright>
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Reflection;
using System.Runtime.Serialization;
using Newtonsoft.Json.Serialization;
using Newtonsoft.Json;
using TrackerRealm.Jazz.Common;
using TrackerRealm.Jazz.Client;

namespace TrackerRealm.Jazz.Json
{
    /// <summary>
    /// 
    /// </summary>
    public class JazzContractResolver : DefaultContractResolver
    {
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="shareCache"></param>
        /// <param name="jObjConverter"></param>
        public JazzContractResolver(bool shareCache, jObjectReferenceConverter jObjConverter)
            : base(false)
        {
            this.jObjConverter = jObjConverter;
        }
        jObjectReferenceConverter jObjConverter;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="objectType"></param>
        /// <returns></returns>
        protected override JsonContract CreateContract(Type objectType)
        {
            JsonContract c= base.CreateContract(objectType);
            if (objectType.IsSubclassOf(typeof(jObject)))  
            {
                c.Converter = jObjConverter;
            }
            else if (objectType == typeof(IjRoleOrProfile))
            {
                c.Converter = jObjConverter;
            }
            return c;
        }
        /// <summary>
        /// Don't serialize properties
        /// </summary>
        /// <param name="objectType"></param>
        /// <returns></returns>
        protected override List<MemberInfo> GetSerializableMembers(Type objectType)
        {
            List<MemberInfo> list = base.GetSerializableMembers(objectType);
            IEnumerable<MemberInfo> membersNoProperties = list.Where(m => !(m is PropertyInfo));

            return new List<MemberInfo>(membersNoProperties);
        }
        //public override JsonContract ResolveContract(Type type)
        //{
        //    return base.ResolveContract(type);
        //}
    }
    //public class JazzContractResolver : IContractResolver
    //{
    //    #region IContractResolver Members

    //    public JsonContract ResolveContract(Type type)
    //    {
    //        throw new NotImplementedException();
    //    }

    //    #endregion
    //}


}
